#include <windows.h>

class CriticalSection {
public:
  CriticalSection() {
    InitializeCriticalSection(&crit_);
	thread_ = 0;
  }
  ~CriticalSection() {
    DeleteCriticalSection(&crit_);
  }
  void Enter() {
    EnterCriticalSection(&crit_);
	thread_ = GetCurrentThreadId();
  }
  void Leave() {
	thread_ = 0;
    LeaveCriticalSection(&crit_);
  }

private:
  CRITICAL_SECTION crit_;
  DWORD thread_;
};

class CritScope {
public:
  CritScope(CriticalSection *pcrit) {
    pcrit_ = pcrit;
    pcrit_->Enter();
  }
  ~CritScope() {
    pcrit_->Leave();
  }
private:
  CriticalSection *pcrit_;
};